var superagent = require('superagent');
var cheerio = require('cheerio');
var url = require('url');
var fs = require('fs');
var http = require('http');
var path = require('path');
var config = require('./config');

var doCatch = function(targeturl,host){
	superagent.get(targeturl).end(function (err, sres) {
	// 常规的错误处理
		if (err) {
			console.log(err.status +'   end !');
			return 
		}else{
			doCatch(config.getNextUrl(targeturl),host);
		}
		var $ = cheerio.load(sres.text);
		var items = [];
		var $dom = $(config.selector);
		$dom.each(function(idx,element){
			var $element = $(element);
			items.push($element.attr('src'));
		});
	
		items.forEach(function(value,idx){
			var newUrl = host + value;
			var fileName = path.basename(newUrl);
	
			/*var request=require('request');
			request(newUrl).on('response', function(response) {
				response.on("end", function(){
					console.log(fileName);
				})
			}).pipe(fs.createWriteStream("./img/" + fileName));*/
	
	
			http.get(newUrl, function(res){
				var imgData = "";
	
				res.setEncoding("binary"); //一定要设置response的编码为binary否则会下载下来的图片打不开
	
	
				res.on("data", function(chunk){
					imgData+=chunk;
				});
	
				res.on("end", function(){
					fs.writeFile("./img/" + fileName, imgData, "binary", function(err){
						if(err){
							console.log("down fail");
						}
						console.log("down success:" + fileName);
					});
				});
			});
		});
	});
};

/*config.targetUrl.forEach(function(value){
	doCatch(value,config.hostname);
});
*/
doCatch(config.targetUrl,config.hostname);



